package com.mcms.service.xs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mcms.util.DBHelper;
import com.mcms.util.MapUtil;

public class XCJZDetailService {
	public static Map execute(Map sessionMap, Map paramMap) {

		Map map = new HashMap();
		
		XCJZDetailService xcjzD = new XCJZDetailService();
		
		// 获取客户基本信息Map
		map.put("basicInfo", xcjzD.getBasicInfo(sessionMap, paramMap));
		
		// 获取客户问诊信息Map
		map.put("inquireInfo", xcjzD.getInquireInfo(sessionMap, paramMap));
		
		// 获取客户就诊记录ID列表
		map.put("doctorInfo", xcjzD.getDoctorList(sessionMap, paramMap));
		
		// 获取客户回访记录ID列表
		map.put("callBackInfo", xcjzD.getCallBackList(sessionMap, paramMap));
		
		// 获取客户图片记录信息列表
		map.put("photoInfo", xcjzD.getPhotoList(sessionMap, paramMap));

		// 返回查询结果列表
		return map;
	}
	
	/**
	 * 获取客户基本信息Map
	 * ( 根据客户id查询用户信息表)
	 * @return
	 */
	private Map getBasicInfo(Map sessionMap, Map paramMap){
		String sql = "select custom_id,custom_name,gender,age,telephone_no,native,fixed_telephone_no,address,EMAIL,qq,profession,work_place from mcms_custom_info where custom_id = '"+paramMap.get("customid")+"'";
		DBHelper dbh = DBHelper.getInstance();
		List listResult =  dbh.query(sql);
		Map<String, String> map = (Map<String, String>) listResult.get(0);
		return map;
	}
	
	/**
	 * 获取客户问诊信息Map
	 * ( 根据客户id查询用户问诊信息表 )
	 * @return
	 */
	private Map getInquireInfo(Map sessionMap, Map paramMap){
		String customid = (String) paramMap.get("customid");
		String doctorid = (String) paramMap.get("doctorid");
		String sql = " select id,custom_subscribe_id,channel,refer_item,refer_doctor,bald_condition,bald_duration,bald_level,is_inherit,is_treat from MCMS_CUSTOM_INQUIRY_INFO where custom_subscribe_id='"+doctorid+"' order by custom_subscribe_id desc";
		DBHelper dbh = DBHelper.getInstance();
		List listResult =  dbh.query(sql);
		if(listResult.size()==0){
			return new HashMap();
		}
		Map<String, String> map = (Map<String, String>) listResult.get(0);
		return map;
	}
	
	/**
	 * 获取客户就诊记录ID列表
	 * (用于显客户就诊记录树)
	 * 1.多次就诊记录的预约就诊号，组成list传回页面
	 * 2.在客户就诊信息页签中的就诊记录树，
	 *   点击记录树节点查询客户就诊表+处置信息表+监测信息表+手术记录表+药物处置表，组织当次就诊的详细信息
	 * @return
	 */
	private List getDoctorList(Map sessionMap, Map paramMap){
		List list = new ArrayList();
		String customid = (String) paramMap.get("customid");
		String sql = "select doctor_id from MCMS_CUSTOM_DOCTOR where custom_id ='"+customid+"'";
		DBHelper dbh = DBHelper.getInstance();
		List listResult =  dbh.query(sql);
		return list;
	}
	
	/**
	 * 获取客户回访记录ID列表
	 * (用于显客户回访记录树)
	 * 1.查询出本客户的各次回访计划表的id，
	 * 2.放入list中传入页面
	 * 3.页面的回访客户信息页签中的回访树中，点击后按照回访计划id，关联查询回访计划表和回访记录表
	 * @return
	 */
	private List getCallBackList(Map sessionMap, Map paramMap){
		List list = new ArrayList();
		String customid = (String) paramMap.get("customid");
		String sql ="select t2.id from MCMS_CALLBACK_PLAN t1,MCMS_CALLBACK_RECORDER t2 where t2.callback_plan_id = t1.id and t1.custom_id = '"+customid+"' order by t2.callback_time asc";
		DBHelper dbh = DBHelper.getInstance();
		List listResult =  dbh.query(sql);
		return list;
	}
	
	/**
	 * 获取客户图片记录信息列表
	 * (用于显示图片列表信息)
	 * 1.按当前客户id，查询图片信息表，按照时间排序，升序
	 * 2.本次上传的图片信息，组装成map，放入list中
	 * 3.list返回页面，在图片页签中遍历显示
	 * @return
	 */
	private List getPhotoList(Map sessionMap, Map paramMap){
		List list = new ArrayList();
		String customid = (String) paramMap.get("customid");
		String sql = "select custom_id,doctor_id,upload_dept_id,url,upload_time,upload_id,remark from MCMS_PICTURE_INFO where custom_id ='"+customid+"' order by upload_time asc ";
		DBHelper dbh = DBHelper.getInstance();
		List listResult =  dbh.query(sql);
		return list;
	}
	
}
